﻿using System;
using System.Collections.Generic;

namespace DreamCube.Foundation.Basic.Utility
{
    /// <summary>
    /// 枚举值
    /// </summary>
    public static class MyEnum
    {
        /// <summary>
        /// 增加缓冲区
        /// </summary>
        private static Cache.DictionaryCachePool<Type, Object> cacheBlock = new Cache.DictionaryCachePool<Type, Object>();

        /// <summary>
        /// 转换成枚举类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumString"></param>
        /// <returns></returns>
#if NET20
        public static T ToEnum<T>(String enumString, Boolean ignoreCase = true) where T : struct
        {
            T tempValue = default(T);

            try
            {
                tempValue = (T)Enum.Parse(typeof(T), enumString, ignoreCase);
            }
            catch (Exception)
            { }
            return tempValue;
        }
#else 
        public static T ToEnum<T>(this String enumString, Boolean ignoreCase = true) where T : struct
        {
            T tempValue = default(T);
            Enum.TryParse<T>(enumString, ignoreCase, out tempValue);
            return tempValue;
        }
#endif

        /// <summary>
        /// 转换成枚举类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumString">枚举项的字符串</param>
        /// <param name="enumValue">返回枚举值</param>
        /// <returns></returns>
#if NET20
        public static Boolean TryToEnum<T>(String enumString, out T enumValue, Boolean ignoreCase = true) where T : struct
        {
            enumValue = default(T);
            Boolean result = false;
            try
            {
                enumValue = (T)Enum.Parse(typeof(T), enumString, ignoreCase);
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
#else
        public static Boolean TryToEnum<T>(this String enumString, out T enumValue, Boolean ignoreCase = true) where T : struct
        {
            enumValue = default(T);
            Boolean result = false;
            result = Enum.TryParse<T>(enumString, ignoreCase, out enumValue);
            return result;
        }
#endif

        /// <summary>
        /// 获取枚举类型的所有枚举项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static List<Objects.EnumItem<T>> GetEnumItems<T>() where T : struct
        {
            Type t = typeof(T);
            //先从缓存中查找
            if (cacheBlock.ContainsKey(t))
            {
                Object enumItems = null;
                if (cacheBlock.TryGetValue(t, out enumItems))
                    return (List<Objects.EnumItem<T>>)enumItems;
            }
            String[] names = Enum.GetNames(t);
            List<Objects.EnumItem<T>> items = new List<Objects.EnumItem<T>>();
            for (Int32 i = 0; i < names.Length; i++)
            {
                Objects.EnumItem<T> item = new Objects.EnumItem<T>();
                item.EnumValue = Enum.Parse(t, names[i]);
                item.IdentityValue = names[i];
                item.Description = MyType.GetFieldDescription(t, names[i]);
                items.Add(item);
            }
            //添加到缓存中
            cacheBlock.TryAdd(t, items);
            return items;
        }
    }
}
